/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.soap.client;

import cz.muni.fi.pa165.soap.client.table.Button;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author shifty
 */
public abstract class TableModel extends AbstractTableModel {
    
    private Object[][] values = new Object[0][0];
    private String[] columns;
    private Class<?>[] columnTypes;
    private JButton editButton;
    private JButton deleteButton;
    
    public static final String EDIT_BUTTON=MainFrame.RB.getString("table.edit");
    public static final String DELETE_BUTTON=MainFrame.RB.getString("table.delete");
    
    public TableModel(String[] columns) {
        this.columns=columns;
        
        Class<?>[] types = new Class<?>[columns.length+1];
        for(int i=0;i<types.length-2;i++) {
            types[i] = String.class;
        }
        types[types.length-2]=JButton.class;
        types[types.length-1]=JButton.class;
        
        this.columnTypes = types;
    }
    
    public String[] getColumns() {
        return columns;
    }

    public void setColumns(String[] columns,Class<?>[] columnTypes) {
        this.columns = columns;
        this.columnTypes = columnTypes;
    }

    public Object[][] getValues() {
        return values;
    }

    public void setValues(Object[][] values) {
        this.values = values;
    }
    
    
    
    
    @Override
    public String getColumnName(int column) {
        return columns[column];
    }
    @Override 
    public Class<?> getColumnClass(int column) {
        return columnTypes[column];
    }
    
    @Override
    public int getRowCount() {
        return values.length;
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }
    
    @Override 
    public Object getValueAt(final int rowIndex, final int columnIndex) {
        if(columnIndex == getColumnCount()-2) {
            editButton = new Button(columns[columnIndex],(Long)getValueAt(rowIndex,0));
            editButton.addActionListener(alEdit);
            return editButton;
        }
        if(columnIndex == getColumnCount()-1) {
            deleteButton = new Button(columns[columnIndex],(Long)getValueAt(rowIndex,0));
            deleteButton.addActionListener(alDelete);
            return deleteButton;
        }
        return values[rowIndex][columnIndex];
    }
    private ActionListener alEdit;
    private ActionListener alDelete;
    public void setActionListenerEdit(ActionListener al) {
        alEdit = al;
    }
    public void setActionListenerDelete(ActionListener al) {
        alDelete = al;
    }
}

